package cyc2018.贪心思想;

/**
 *  买卖股票的最佳时机
 */
public class Test121 {

    // 遍历数组，找到最小的 price，寻找的过程中求解当前最小 price 下的最大收益
    public static int maxProfit(int[] prices) {
        if (prices.length == 0) {
            return 0;
        }
        int maxProfit = 0;
        int min = prices[0];
        for (int i = 1; i < prices.length; i++) {
            if (prices[i] < min) {
                min = prices[i];
            } else {
                maxProfit = Math.max(maxProfit, prices[i] - min);
            }
        }
        return maxProfit;
    }

    public static void main(String[] args) {
//        int[] a = {7,1,5,3,6,4};
        int[] a = {7,6,4,3,1};
        System.out.println(maxProfit(a));
    }
}
